<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\admin\controller;

use app\src\category\logic\CategoryLogic;
use app\src\goods\logic\SkuLogic;
use app\src\goods\logic\SkuvalueLogic;
use think\Log;

class CategorySku extends Admin{
	
	protected $level;
	protected $parent;
	protected $preparent;
	
	public function index(){

		$cate_id = $this->_param('cate_id',-1);
		$map = array(
			'cate_id'=>$cate_id
		);
		$name = $this->_param('name','');
        $params = array(
			'cate_id'=>$cate_id
		);


        $page = array('curpage' => $this->_param('p', 0), 'size' => config('LIST_ROWS'));

		$order = " id asc ";
//        dump($cate_id);
        $result = (new CategoryLogic())->getInfo(["id" => $cate_id]);
//        dump($result);
//        exit;
		if(!$result['status']){
			$this->error($result['info']);
		}

        $cate_vo = $result['info'];

		$result = (new SkuLogic())->queryWithPagingHtml($map,$page,$order,$params);

		if($result['status']){
			$this->assign('cate_id',$cate_id);
			$this->assign('cate_vo',$cate_vo);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			return $this->boye_display();
		}else{
			Log::record('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}
	
	public function add(){
		$cate_id = $this->_param('cate_id',-1);
		if(IS_GET){

            $this->assign('cate_id',$cate_id);
            return $this->boye_display();
		}else{
			$name = $this->_param('name','');

            if(empty($name)){
				$this->error("属性不能为空！");
			}

            $entity = array(
				'cate_id'=>$cate_id,
				'name'=>$name,
			);

//			$result = apiCall(SkuApi::ADD,array($entity));
			$result = (new SkuLogic())->add($entity);

			if($result['status']){
				$this->success("添加成功！",url('Admin/CategorySku/index',array('cate_id'=>$cate_id,'preparent'=>$this->preparent,'parent'=>$this->parent,'level'=>  $this->level)));
			}
			else{
				$this->error($result['info']);
			}

        }
	}

    public function edit(){
		$cate_id = $this->_param('cate_id',-1);
		$id = $this->_param('id','');

        if(IS_GET){
			$result = (new SkuLogic())->getInfo(['id'=>$id]);

			if($result['status']){
				$this->assign("vo",$result['info']);
			}
			$this->assign('cate_id',$cate_id);
            return $this->boye_display();
		}else{
			$name = $this->_param('name','');

            if(empty($name)){
				$this->error("属性不能为空！");
			}

            $entity = array(
				'name'=>$name,
			);

//			$result = apiCall(SkuApi::SAVE_BY_ID,array($id,$entity));
			$result = (new SkuLogic())->saveByID($id,$entity);


            if($result['status']){
				$this->success("保存成功！",url('Admin/CategorySku/index',array('cate_id'=>$cate_id,'preparent'=>$this->preparent,'parent'=>$this->parent,'level'=>$this->level)));
			}
			else{
				$this->error($result['info']);
			}

        }
	}

	public function delete(){

        $id = $this->_param('id',0);
		$map = array('sku_id'=>$id);
//		$result = apiCall(SkuvalueApi::QUERY_NO_PAGING,array($map));
		$result = (new SkuvalueLogic())->queryNoPaging($map);
		if($result['status']){
			if(count($result['info']) > 0){
                $this->error("存在SKU值，请先删除SKU值！");
			}
			$result = (new SkuLogic())->delete(['id'=>$id]);
			if($result['status']){
				$this->success("删除成功！");
			}else{
				$this->error($result['info']);
			}

        }else{

        }
	}

    protected function _initialize()
    {
        parent::_initialize();
        $this->level = $this->_param('level', 0);
        $this->parent = $this->_param('parent', 0);
        $this->preparent = $this->_param('preparent', 0);

        $this->assign("level", $this->level);
        $this->assign("parent", $this->parent);
        $this->assign("preparent", $this->preparent);
    }
}
